// 滑动窗口--->长度最小的子数组
class Solution {
    public:
        int minSubArrayLen(int target, vector<int>& nums) {
        int left = 0;
        int right = 0;
        int n = nums.size();
        int sum = 0;
        int len = 0;
        for (right = 0; right < n; right++)
        {
            sum += nums[right];
            len++;
            if (sum >= target)
            {
                break;
            }
        }
        if (sum < target) return 0;
       
        for (left = 0; left <= right; left++)
        {
            if (sum >= target)
            {
                len = min((right - left + 1), len);
            }
            sum -= nums[left];
            while (right +1 < n && sum < target)
            {
                right++;
                sum += nums[right];
            }
    
        }
        return len;
        }
    };


    class Solution {
        public:
            int minSubArrayLen(int target, vector<int>& nums) {
                int left = 0; int len = INT_MAX;
                int n = nums.size();
                int sum = 0;
                for(int right = 0; right < n; right++)
                {
                    sum += nums[right];
                    while(sum >= target)
                    {
                        len = min(len,right - left + 1);
                        sum -= nums[left++];
                    }
                }
        
                return len == INT_MAX ? 0 : len;
            }
        };

        // 无重复字符的最长子串
        class Solution {
            public:
                int lengthOfLongestSubstring(string s) {
                    int n = s.size();
                    int left  = 0;
                    unordered_set<char>hash;
                    int ret = 0;
                    for(int right = 0; right < n; right++)
                    {
                        while(hash.count(s[right]) != 0)
                        {
                            hash.erase(s[left++]);
                        }
                        hash.insert(s[right]);
                        ret = max(ret,right - left + 1);
                    }
                    return ret;
                }
            };

            // 最大连续1的个数三
            class Solution {
                public:
                    int longestOnes(vector<int>& nums, int k) {
                        int left  = 0;
                        int zero = 0;
                        int len = 0;
                        int n = nums.size();
                        for(int right = 0; right < n; right++)
                        {
                            if(nums[right] == 0)
                                zero++;
                            while(zero > k)
                            {
                                if(nums[left] == 0)
                                {
                                    zero--;
                                }
                                left++;
                            }
                            len = max(right - left + 1,len);
                        }
                
                
                        return len;
                    }
                };